import 'package:flutter/material.dart';
import 'package:razer_one/app/main/constants/color_constants.dart';
import 'package:razer_one/app/main/view/main_text.dart';

enum BtnType {
  imageLeft, //按钮图片在左边
  imageRight, //按钮图片在右边
  imageTop, //按钮图片在上面 =  默认
  imageBottom, //按钮图片在下面
}

class MainTextBtn extends StatelessWidget {
  final GestureTapCallback handle;
  final String btnName;
  final Color textNormalColor;
  final Color textSelectColor;
  final FontWeight fontWeight;
  final double? btnW;
  final double? btnH;
  final double btnFonsize;
  final Color backgroundNormalColor;
  final Color backgroundSelectColor;
  final Color borderSideColor;
  final bool isShowBoder; //圆角
  final double? borderRadius;
  final bool isShowSide; //边框
  final bool isSelect;
  final Alignment alignment;
  final EdgeInsets? padding;
  const MainTextBtn(
    this.btnName, {
    Key? key,
    required this.handle,
    this.textNormalColor = Colors.black,
    this.textSelectColor = Colors.white,
    this.fontWeight = FontWeight.normal,
    this.btnW,
    this.btnH = 40,
    this.btnFonsize = 16,
    this.backgroundNormalColor = Colors.white,
    this.backgroundSelectColor = Colors.red,
    this.borderSideColor = Colors.grey,
    this.isShowBoder = false,
    this.borderRadius,
    this.isShowSide = false,
    this.isSelect = false,
    this.alignment = Alignment.center,
    this.padding = EdgeInsets.zero,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: btnW,
      height: btnH,
      child: TextButton(
        style: ButtonStyle(
          padding: MaterialStateProperty.all(padding),
          side: isShowSide
              ? MaterialStateProperty.all(
                  BorderSide(color: borderSideColor, width: 0.5),
                )
              : null,
          alignment: alignment,
          //圆角
          shape: MaterialStateProperty.all(
            RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(isShowBoder
                    ? (borderRadius ?? (btnH != null ? (btnH! / 2) : 16))
                    : 0)),
          ),
          backgroundColor: MaterialStateProperty.all<Color>(
              !isSelect ? backgroundNormalColor : backgroundSelectColor),
        ),
        //边框
        onPressed: handle,
        child: Text(btnName,
            maxLines: 1,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: btnFonsize,
              color: !isSelect ? textNormalColor : textSelectColor,
              fontWeight: fontWeight,
            )),
      ),
    );
  }
}

//提交按钮
class MainCommitBtn extends StatelessWidget {
  final GestureTapCallback handle;
  final String btnName;
  final double? btnW;
  final double? btnH;
  final double btnFonsize;
  final double? borderRadius;
  final Color backgroundNormalColor;
  const MainCommitBtn(
    this.btnName, {
    Key? key,
    required this.handle,
    this.btnW,
    this.btnH = 40,
    this.btnFonsize = 16,
    this.borderRadius = 5,
    this.backgroundNormalColor = const Color(kBtnColor),
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MainTextBtn(
      btnName,
      handle: handle,
      btnH: btnH,
      btnW: btnW,
      isShowBoder: true,
      borderRadius: borderRadius,
      backgroundNormalColor: const Color(kBtnColor),
      textNormalColor: Colors.white,
      btnFonsize: btnFonsize,
    );
  }
}

//取消按钮
class MainCancelBtn extends StatelessWidget {
  final GestureTapCallback handle;
  final String btnName;
  final double? btnW;
  final double? btnH;
  final double btnFonsize;
  const MainCancelBtn(
    this.btnName, {
    Key? key,
    required this.handle,
    this.btnW,
    this.btnH,
    this.btnFonsize = 15,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MainTextBtn(
      btnName,
      handle: handle,
      btnH: btnH,
      btnW: btnW,
      isShowBoder: true,
      borderRadius: 5,
      backgroundNormalColor: const Color(kBackGroundColor),
      textNormalColor: const Color(kLightBlueColor),
      btnFonsize: btnFonsize,
    );
  }
}

//边框按钮
class MainSideBtn extends StatelessWidget {
  final GestureTapCallback handle;
  final String btnName;
  final double? btnW;
  final double? btnH;
  final double btnFonsize;
  final bool isSelect;
  const MainSideBtn(
    this.btnName, {
    Key? key,
    required this.handle,
    this.btnW,
    this.btnH,
    this.btnFonsize = 15,
    this.isSelect = false,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MainTextBtn(
      btnName,
      handle: handle,
      isShowSide: true,
      borderSideColor: const Color(kBtnColor),
      textNormalColor: const Color(kBtnColor),
      backgroundSelectColor: const Color(kBtnColor),
      isSelect: isSelect,
      btnW: btnW,
    );
  }
}

//请选择收款方式箭头按钮
class MainSelectBtn extends StatefulWidget {
  String btnName;
  final GestureTapCallback handle;
  final FontWeight fontWeight;
  final double? btnW;
  final double? btnH;
  final double btnFonsize;
  MainSelectBtn(
    this.btnName, {
    Key? key,
    required this.handle,
    this.fontWeight = FontWeight.normal,
    this.btnW,
    this.btnH,
    this.btnFonsize = 15,
  }) : super(key: key);

  @override
  State<MainSelectBtn> createState() => MainSelectBtnState();
}

class MainSelectBtnState extends State<MainSelectBtn> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: widget.btnW,
      height: widget.btnH,
      child: TextButton(
        style: ButtonStyle(
          alignment: Alignment.centerLeft,
          backgroundColor:
              MaterialStateProperty.all<Color>(const Color(kBackGroundColor)),
        ),
        //边框
        onPressed: widget.handle,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Text(widget.btnName,
                  maxLines: 1,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: widget.btnFonsize,
                    color: const Color(kBlack1FontColor),
                    fontWeight: widget.fontWeight,
                  )),
            ),
            Image.asset("lib/app/resource/images/commond/commond_arrow.png"),
          ],
        ),
      ),
    );
  }

  updateBtnName(String title) {
    setState(() {
      widget.btnName = title;
    });
  }
}

class MainImageBtn extends StatelessWidget {
  final BtnType btntype;
  final String btnName;
  final String imageName;
  final Color textColor;
  final GestureTapCallback onp;
  final double btnW;
  final double btnH;
  final double btnFonsize;
  final bool isShowBorder; //是否展示右上角红点
  final Color boderColor;
  final bool isShowRed;
  const MainImageBtn(this.btnName, this.imageName, this.onp,
      {Key? key,
      this.btntype = BtnType.imageTop,
      this.btnW = 70,
      this.btnH = 50,
      this.textColor = Colors.black,
      this.btnFonsize = 15,
      this.isShowBorder = false,
      this.isShowRed = false,
      this.boderColor = Colors.black})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    //图片在上面
    if (btntype == BtnType.imageTop) {
      return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: onp,
        child: Container(
          width: btnW,
          height: btnH,
          // color: Colors.red,
          decoration: isShowBorder
              ? BoxDecoration(
                  borderRadius: BorderRadius.circular(5), //设置圆角
                  border: Border.all(
                      width: 1, // 设置边框的宽度
                      color: boderColor // 设置边框的颜色
                      ))
              : null,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Flexible(
                child: !isShowRed
                    ? Image.asset(
                        imageName,
                      )
                    : Stack(
                        children: [
                          Image.asset(
                            imageName,
                          ),
                          const Positioned(
                            top: 0,
                            right: 0,
                            child: CircleAvatar(
                              radius: 5,
                              backgroundColor: Colors.red,
                              child: MainText(
                                "",
                              ),
                            ),
                          )
                        ],
                      ),
              ),
              const SizedBox(
                height: 5,
              ),
              Flexible(
                child: MainText(btnName, fontSize: btnFonsize),
              ),
            ],
          ),
        ),
      );
    } else {
      return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: onp,
        child: SizedBox(
          width: btnW,
          height: btnH,
          // color: Colors.red,
          child: Row(
            // mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Flexible(
                child: !isShowRed
                    ? Image.asset(
                        imageName,
                      )
                    : Stack(
                        children: [
                          Image.asset(
                            imageName,
                          ),
                          Positioned(
                            top: 0,
                            left: 0,
                            child: CircleAvatar(
                              radius: 5,
                              backgroundColor: Colors.red,
                              child: MainText(
                                "",
                                textColor: textColor,
                              ),
                            ),
                          )
                        ],
                      ),
              ),
              sizeW(2),
              MainText(
                btnName,
                textColor: textColor,
              )
            ],
          ),
        ),
      );
    }
  }
}
